package com.zhijian.wharf.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import com.zhijian.wharf.dao.AisMapper;
import com.zhijian.wharf.entity.po.Ais;
import com.zhijian.wharf.entity.vo.request.AisSaveOrUpdateVo;
import com.zhijian.wharf.entity.vo.request.AisSearchVo;
import com.zhijian.wharf.entity.vo.response.AisVo;
import com.zhijian.wharf.service.AisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 船舶AIS数据业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-07-11 22:16:22
 */
@Slf4j
@Service
public class AisServiceImpl implements AisService {

    @Autowired
    private AisMapper mapper;

    @Override
    public Ais selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AisVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(Ais ais) {
        Date now = new Date();
        ais.setCreateTime(now);
        ais.setUpdateTime(now);
        return mapper.insert(ais) > 0 ? ais.getId() : 0L;
    }

    @Override
    public Long insertSelective(Ais ais) {
        Date now = new Date();
        ais.setCreateTime(now);
        ais.setUpdateTime(now);
        return mapper.insertSelective(ais) > 0 ? ais.getId() : 0L;
    }

    @Override
    public boolean update(Ais ais) {
        Date now = new Date();
        ais.setUpdateTime(now);
        return mapper.updateByPrimaryKey(ais) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Ais record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(AisSaveOrUpdateVo saveOrUpdateVo) {
        Ais ais = new Ais();
        BeanUtils.copyProperties(saveOrUpdateVo, ais);
        return this.insert(ais) != 0;
    }

    @Override
    public boolean modify(AisSaveOrUpdateVo saveOrUpdateVo) {
        Ais old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        Ais ais = new Ais();
        BeanUtils.copyProperties(saveOrUpdateVo, ais);
        return this.update(ais);
    }

    @Override
    public AisVo selectOne(AisSearchVo aisSearchVo) {
        return mapper.selectOne(aisSearchVo);
    }

    @Override
    public Integer count(AisSearchVo aisSearchVo) {
        return mapper.count(aisSearchVo);
    }

    @Override
    public List<AisVo> selectList(AisSearchVo aisSearchVo) {
        return mapper.selectList(aisSearchVo);
    }

    @Override
    public Page<AisVo> selectPage(AisSearchVo aisSearchVo) {
        AssertUtil.check(Objects.isNull(aisSearchVo.getPageNum()) || Objects.isNull(aisSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(aisSearchVo);
        //分页
        Page<AisVo> page = new Page<>(true, count, aisSearchVo.getPageNum(), aisSearchVo.getPageSize());
        aisSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AisVo> responseVos = this.selectList(aisSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<Ais> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(AisSearchVo aisSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(aisSearchVo);
    }

}